package com.bizfx.component.fields
{
	import flash.events.Event;
	
	import com.bizfx.component.views.IDetailView;
	import com.bizfx.utils.ObjectUtils;
	import com.bizfx.utils.StringUtils;
	
	import mx.controls.NumericStepper;
	import mx.events.ValidationResultEvent;
	import mx.utils.UIDUtil;
	import mx.validators.NumberValidator;
	
	public class NumericStepperField extends NumericStepper implements IField, IValidationField
	{
		private var _validator:NumberValidator;
		
		public function NumericStepperField()
		{
			super();
			this.minWidth = 160;
			this._validator = new NumberValidator();
			this._validator.required = false;
			this._validator.requiredFieldError = "必填";
			this.setStyle("backgroundDisabledColor", 0xFFFFFF);
			this.setStyle("disabledOverlayAlpha", 0);
			this.setStyle("disabledColor", 0x000000);
			this.setStyle("textAlign", "right");
			this.addEventListener(Event.CHANGE, fieldChangeHandler);
			this.maximum = int.MAX_VALUE;
		}
		
		private function fieldChangeHandler(event:Event):void {
			this.data = this.value;
		}
		
		public function get fieldKey():String
		{
			return this.id;
		}
		
		public function set fieldKey(value:String):void
		{
			id = value;
		}
		
		private var _bindProperty:String;
		[Bindable]
		public function get bindProperty():String
		{
			return _bindProperty;
		}
		
		public function set bindProperty(value:String):void {
			_bindProperty = value;
		}
		
		public function validate():void {
			this._validator.source = this;
			this._validator.property = "value";
			var event:ValidationResultEvent = this._validator.validate(this.value);
			this.dispatchEvent(event);
		}
		
		[Bindable]
		public function get required():Boolean
		{
			return this._validator.required;
		}
		
		public function set required(value:Boolean):void {
			this._validator.required = value;
		}
		
		override public function set data(value:Object):void {
			if (value != null) {
				super.value = value as Number;
				super.data = value;
			} else {
				super.value = 0;
				super.data = 0;
			}
		}
		
//		private var _view:IDetailView;
//		[Bindable]
//		public function get view():IDetailView
//		{
//			return this._view;
//		}
//		
//		public function set view(value:IDetailView):void {
//			if (StringUtils.isNullOrEmpty(this.id)) {
//				this.id = UIDUtil.createUID();
//			}
//			if (this._view !== value) {
//				if (_view) {
//					_view.removeFieldByKey(this.fieldKey);
//				}
//				this._view = value;
//				if (_view) {
//					_view.addField(this);
//				}
//			}
//		}

		private var _editable:Boolean = true;
		
		public function get editable():Boolean {
			return this._editable;
		}
		
		public function set editable(value:Boolean):void {
			this._editable = value;
		}
		
		private var _enabled:Boolean = true;
		
		override public function get enabled():Boolean
		{
			return this._enabled;
		}
		
		override public function set enabled(value:Boolean):void {
			this._enabled = value;
			if (this.editable) {
				super.enabled = value;
			} else {
				super.enabled = false;
			}
		}
		
		[Inspectable(category="General", enumeration="true,false", defaultValue="false")]
		[Bindable("readOnlyChanged")]
		public function get readOnly():Boolean {
			return this.enabled;
		}
		
		public function set readOnly(value:Boolean):void {
			if (this.enabled == value) {
				this.enabled = !value;
				this.dispatchEvent(new Event("readOnlyChanged"));
			}
		}
	}
}